﻿/**
 * 将图片按比例缩放
 * 
 * @author Luodong
 * @email luo4521@online.net.tw
 * 2010-2-4 22:32
 */
package com.addcn.texas.utils
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;	
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	
	public class  PicBitMap
	{
		private var _bitmap : Bitmap;
		
		public function PicBitMap() : void { };
		/**
		 * 
		 * @param	r_Object DispalyObject 图片对象
		 * @param	r_width Number         要缩放大宽度
		 * @param	r_height Number        要缩放的高度
		 * @param	r_cut Boolean          是否裁剪，默认false
		 * @param	r_translate Boolean    是否將圖片居中
		 * @return  Bitmap
		 */
		public static function getPicBitMap(r_Object : DisplayObject ,r_width : Number, r_height : Number, r_cut: Boolean = false,r_translate = true) : Bitmap
		{			
			var _rect:Rectangle = r_Object.getBounds(r_Object);
			var _per_width = r_width / _rect.width;
			var _per_height = r_height / _rect.height;
			var _scale : Number;
			if (!r_cut) {
				_scale = (_per_width <= _per_height) ? _per_width : _per_height;
			}else {
				_scale = (_per_width <= _per_height) ? _per_height : _per_width;
			}						
			var _bmp:BitmapData = new BitmapData(r_width, r_height, true, 0);
			var _matrix:Matrix = new Matrix();
			_matrix.scale(_scale, _scale);
			if (r_translate) {
				var _offer_w = (r_width - _rect.width * _scale) / 2;
				var _offer_h = (r_height - _rect.height * _scale) / 2;
				_matrix.translate( _offer_w, _offer_h);
			}
			_bmp.draw(r_Object, _matrix);
			//if (r_Object is Bitmap) {
				//(r_Object as Bitmap).bitmapData.dispose();
			//}
			var _pic : Bitmap = new Bitmap(_bmp);
			r_Object = null;
			return _pic;
		}				
	}
	
}
